package com.eedi.framework.permission.controller.org;

import com.eedi.framework.common.enums.YesOrNoEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.permission.controller.org.vo.menu.OrgMenuListReq;
import com.eedi.framework.permission.controller.org.vo.menu.OrgMenuResp;
import com.eedi.framework.permission.controller.org.vo.menu.OrgMenuSaveReq;
import com.eedi.framework.permission.controller.org.vo.menu.OrgMenuSimpleResp;
import com.eedi.framework.permission.convert.OrgMenuConvert;
import com.eedi.framework.permission.dal.dataobject.OrgMenuDO;
import com.eedi.framework.permission.service.OrgPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;

/**
 * @author liuyf
 */
@Tag(name = "经营界面 - 菜单管理")
@RestController
@RequestMapping("/org-menu")
@Validated
public class OrgMenuController {

    @Resource
    private OrgPermissionService orgPermissionService;

    @PostMapping("/create")
    @Operation(summary = "创建菜单(org:menu:create)")
    @PreAuthorize("@org.hasPermission('org:menu:create')")
    public CommonResult<String> createMenu(@Valid @RequestBody OrgMenuSaveReq createReqVO) {
        createReqVO.setOrgMenuKeepAlive(YesOrNoEnum.YES);
        createReqVO.setOrgMenuAlwaysShow(YesOrNoEnum.YES);
        createReqVO.setOrgMenuVisible(YesOrNoEnum.YES);
        String menuId = orgPermissionService.createMenu(createReqVO);
        return success(menuId);
    }

    @PutMapping("/update")
    @Operation(summary = "修改菜单(org:menu:update)")
    @PreAuthorize("@org.hasPermission('org:menu:update')")
    public CommonResult<Boolean> updateMenu(@Valid @RequestBody OrgMenuSaveReq updateReqVO) {
        orgPermissionService.updateMenu(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除菜单(org:menu:delete)")
    @Parameter(name = "orgMenuId", description = "角色编号", required= true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:menu:delete')")
    public CommonResult<Boolean> deleteMenu(@RequestParam("orgMenuId") String orgMenuId) {
        orgPermissionService.deleteMenu(orgMenuId);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取菜单列表(org:menu:list)", description = "用于【菜单管理】界面")
    @PreAuthorize("@org.hasPermission('org:menu:list')")
    public CommonResult<List<OrgMenuResp>> getMenuList(OrgMenuListReq reqVO) {
        List<OrgMenuDO> list = orgPermissionService.getMenuList(reqVO);
        list.sort(Comparator.comparing(OrgMenuDO::getOrgMenuSort));
        List<OrgMenuResp> beans = BeanUtils.toBean(list, OrgMenuResp.class);
        return success(OrgMenuConvert.INSTANCE.buildMenuTree(beans));
//        return success(BeanUtils.toBean(list, OrgMenuResp.class));
    }

    @GetMapping({"/list-all-simple"})
    @Operation(summary = "获取菜单精简信息列表", description = "只包含被开启的菜单，用于【角色分配菜单】功能的选项。" +
            "在多租户的场景下，会只返回租户所在套餐有的菜单")
    public CommonResult<List<OrgMenuSimpleResp>> getSimpleMenuList() {
        List<OrgMenuDO> list = orgPermissionService.getMenuListByTenant(new OrgMenuListReq() );
        list.sort(Comparator.comparing(OrgMenuDO::getOrgMenuSort));
        List<OrgMenuSimpleResp> bean = BeanUtils.toBean(list, OrgMenuSimpleResp.class);
        return success(OrgMenuConvert.INSTANCE.buildMenuSimpleTree(bean));

    }

    @GetMapping("/get")
    @Operation(summary = "获取菜单信息(org:menu:query)")
    @PreAuthorize("@org.hasPermission('org:menu:query')")
    public CommonResult<OrgMenuResp> getMenu(String menuId) {
        OrgMenuDO menu = orgPermissionService.getMenu(menuId);
        return success(BeanUtils.toBean(menu, OrgMenuResp.class));
    }

}
